/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import java.sql.*;

import ProjectMgtSystem.Utilities.*;
import ProjectMgtSystem.Entities.*;
import ProjectMgtSystem.Bean.LoginForm;
import java.util.ArrayList;
import org.apache.catalina.Session;

/**
 *
 * @author Nguyen Phu Cuong
 *  Create date: 2011/07/12
 *  Updated latest by: Su Duy Hien 2011/07/18
 */
public class LoginFormAction extends org.apache.struts.action.Action {
    /* forward name="success" path="" */
    private static final String FAILURE = "failure";
    private static final String ADMIN = "admin";
    private static final String USER = "user";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // extract user data
        LoginForm loginForm = (LoginForm) form;
        String name = (String) loginForm.get("userName");
        String password = (String) loginForm.get("password");
        // perform validation
       
        User user = new User();
        String message = "";
        user = loginForm.checkLogin(name, password);
        request.setAttribute("errorMessage",user.getMessage());
       
        if (user.getUserid() != null) {
            if (user.getUsertype() == 1) {
                request.setAttribute("USERID", user.getUserid());
                return mapping.findForward(ADMIN);
            } else {
                request.setAttribute("USERID", user.getUserid());
                return mapping.findForward(USER);
            }
        }
        else
        {
            return mapping.findForward(FAILURE);
        }
    }
}
